const express = require('express')
const {
	login,
	logout,
	Response,
	userLoginFilter,
	list,
	userAdd,
	userModify,
	userDel,
} = require('../modules/module-user')

const router = express.Router()

// /cms/user/login
router.post('/user/login', function(req, res, next) {
	return login(req).then(response => {
		return res.type('json').send(response)
	}).catch(err => {
		console.log('---err---', err)
		return res.type('json').send(new Response('102', '错误，登录失败！'))
	})
})

// /cms/user/logout
router.post('/user/logout', function(req, res, next) {
	return logout(req).then(response => {
		return res.type('json').send(response)
	})
})

// /cms/user/list
router.post('/user/list', userLoginFilter, function(req, res, next) {
	return list(req).then(response => {
		return res.type('json').send(response)
	})
})

// /cms/user/add
router.post('/user/add', userLoginFilter, function(req, res, next) {
	return userAdd(req).then(response => {
		return res.type('json').send(response)
	})
})

// /cms/user/modify
router.post('/user/modify', userLoginFilter, function(req, res, next) {
	return userModify(req).then(response => {
		return res.type('json').send(response)
	})
})

// /cms/user/del
router.post('/user/del', userLoginFilter, function(req, res, next) {
	return userDel(req).then(response => {
		return res.type('json').send(response)
	})
})

// /cms/user/cache
router.post('/user/cache', userLoginFilter, function(req, res, next) {
	return res.type('json').send(req.session._userInfo)
})

module.exports = router